package com.lcredu;

import com.lcredu.entity.Dog;
import com.lcredu.entity.Pig;
import com.lcredu.service.T1Service;

/**
 * @author 黎长容
 * @date 2025-07-04 15:31
 */
public class ThreadLocalTest {

    public static ThreadLocal<Object> threadLocal01 = new ThreadLocal();

    /**
     *     public void set(T value) {
     *          获取当前线程t
     *         Thread t = Thread.currentThread();
     *         根据这个线程对象,获取到ThreadLocalMap
     *         ThreadLocalMap map = getMap(t);
     *         如果这个map不为空,就将数据放入;map的key为调用对象(这里是threadLocal01),value值为 要存放的数据
     *         如果为空,就创建一个和当前线程相关联的ThreadLocalMap,并将值放入
     *         if (map != null) {
     *             map.set(this, value);
     *         } else {
     *             createMap(t, value);
     *         }
     *     }
     */
    
    public static class Task implements Runnable {
        @Override
        public void run() {
            Dog dog = new Dog();
            Pig pig = new Pig();
            threadLocal01.set(dog);
            System.out.println("在 run 方法中 线程=" + Thread.currentThread().getName() + "放入了一只dog" + dog);
            new T1Service().update();
        }
    }

    public static void main(String[] args) {

        //启动一个新的线程,注意不是主线程
        new Thread(new Task()).start();
    }
}
